// rem
(function(doc,win){  
    var docEl =doc.documentElement,  
            resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',  
            recalc = function(){  
                var clientWidth = docEl.clientWidth>750 ? 750 :docEl.clientWidth;  
                if (!clientWidth) {return};  
                docEl.style.fontSize = 100 * (clientWidth/750) + 'px';  
            };  
    recalc();  
    if (!doc.addEventListener) return;  
    win.addEventListener(resizeEvt, recalc, false);  
    doc.addEventListener('DOMContentLoaded', recalc, false);  
})(document,window)  

// 选择器
function dqA(selector) {
	return document.querySelectorAll(selector);
};

function dq(selector) {
	return document.querySelector(selector);
};
HTMLElement.prototype.show = function() {
	this.style.display = "block";
	return this;
};
HTMLElement.prototype.hide = function() {
	this.style.display = "none";
	return this;
};

var jtFn = {
	// 跳转页面
	successJump: function(url, mothed) {
		window.location[mothed || "replace"](url || "./");
	},
}

var jtApp = {
    version: "1.0.0", // 版本
	apiOption: {
		rootApi: "", // ajax
	},
	urlOption: {
		index: "./index.html", // app根目录
		chair:"./chair.html"
    },
    // 引用文件立刻执行
	init: function() {
		mui.init();
		
    }(),
    //首页js
    indexHtml: function(){
		var theHtml = {
			init: function() {
				mui.init();
			},
		}
		theHtml.init();
		
		
        // 温度改变
        mui("#home-footer").on("swipedown",".leftTemperature,.rightTemperature",function(){
			// console.log("你向下滑动了");
			var _this = this;
			temperature(_this.classList[1],"add");
		})
		
		mui("#home-footer").on("swipeup",".leftTemperature,.rightTemperature",function(){
			// console.log("你向上滑动了");
			var _this = this;
			temperature(_this.classList[1],"reduce");
		})

		mui("#home-footer").on("tap",".one",function(){
			dq(".swiperBox").show();
			dq(".swiperBox2").hide();
		})

		mui("#home-footer").on("tap",".nine",function(){
			dq(".swiperBox").hide();
			dq(".swiperBox2").show();
		})
		mui("#home-footer").on("tap",".three",function(){
			jtFn.successJump(jtApp.urlOption.chair);
		})

		// 温度加减方法
		function temperature(className,state){
			var temp = document.getElementsByClassName(className);
			var num = parseFloat(temp[0].childNodes[1].innerText);
			if(state == "add"){
				num++;
			}else{
				num--;
			}
			temp[0].childNodes[1].innerText = num;
		}

	
		// 地图控件 缩放放大功能
		mui(".mapZoom").on("tap",".icon-hao,.icon-sub",function(){
			// event.stopPropagation()
			// event.preventDefault()
	
			var _this = this;
			if(_this.getAttribute("data-state") == "add"){
				map.zoomIn()
			}else{
				map.zoomOut()
			}		
		})

		
		
		//地图
		var map = new BMap.Map("allmap");
		var point = new BMap.Point(121.513122,31.105516);
		var mk,pt,myIcon,marker2,gotoAddress;
		var ptt = [];
		// var point = new BMap.Point(121.48789949,31.24916171);

        map.centerAndZoom(point,18);
        var geolocation = new BMap.Geolocation();
        geolocation.getCurrentPosition(function(r){
            if(this.getStatus() == BMAP_STATUS_SUCCESS){
				mk = new BMap.Marker(r.point);
				map.addOverlay(mk)
                map.panTo(r.point);
                
				// console.log(r.point.lng+","+r.point.lat)
				pt = r.point;
                myIcon = new BMap.Icon("images/mapLogo.png", new BMap.Size(60,60));
                marker2 = new BMap.Marker(pt,{icon:myIcon});  // 创建标注
                map.addOverlay(marker2);     
				map.enableScrollWheelZoom(true);
			
            }
            else {
                alert('failed'+this.getStatus());
            }        
		},{enableHighAccuracy: true});

		var bottom_left_control = new BMap.ScaleControl({anchor: BMAP_ANCHOR_BOTTOM_LEFT,offset:new BMap.Size(40,10)});// 左上角，添加比例尺
		
		map.addControl(bottom_left_control);  
		
		// 搜索功能
		function search (){
			var ac = new BMap.Autocomplete({    //建立一个自动完成的对象
					"input" : "suggestId",
					"location" : map
			});
		
			ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
			var str = "";
				var _value = e.fromitem.value;
				var value = "";
				if (e.fromitem.index > -1) {
					value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
				}    
				str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
				
				value = "";
				if (e.toitem.index > -1) {
					_value = e.toitem.value;
					value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
				}    
				str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
				dq("#searchResultPanel").innerHTML = str;
				gotoAddress = [_value.province,_value.city,_value.district,_value.street,_value.business];
				
			});
		
			var myValue;
			ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
			var _value = e.item.value;
				myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
				dq("#searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
				gotoAddress = [_value.province,_value.city,_value.district,_value.street,_value.business];
				
				setPlace();
			});
		
			function setPlace(){
				map.clearOverlays();    //清除地图上所有覆盖物
				function myFun(){
					var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
					map.centerAndZoom(pp, 18);
					map.addOverlay(new BMap.Marker(pp));    //添加标注
				}
				var local = new BMap.LocalSearch(map, { //智能搜索
				  onSearchComplete: myFun
				});
				local.search(myValue);
			}

		}
		search();
		// 导航
		mui(".search").on("tap",".mui-icon-search",function(){
			if(gotoAddress != undefined){
				var myGeo = new BMap.Geocoder();
				// 将地址解析结果显示在地图上,并调整地图视野
				var address =  gotoAddress.join("");
				// console.log(address)
				myGeo.getPoint(address, function(point){
					if (point) {
						console.log(point)
						var p1 = new BMap.Point(pt.lng,pt.lat);
						var p2 = new BMap.Point(point.lng,point.lat);
						// console.log("p1 =",p1,"p2 = ",p2)
						var driving = new BMap.DrivingRoute(map, {renderOptions:{map: map, autoViewport: true}});
						driving.search(p1, p2);
					}else{
						alert("您选择地址没有解析到结果!");
					}
				}, gotoAddress[1]);
			}
		
		});
		
		// swiper
		// var swiper = new Swiper('.swiper-container', {
		// 	slidesPerView: 1,
		// 	spaceBetween: 30,
		// 	loop: true,
		// 	pagination: {
		// 	  el: '.swiper-pagination',
		// 	//   clickable: true,
		// 	}
		//   });
	
	},
	chairHtml: function(){
		var theHtml = {
			init: function() {
				mui.init();
			},
		}
		theHtml.init();

		// 调节座椅
		
		var tl1 = new TimelineMax();
		var deg = 0;
		var deg2 = 0;
		mui(".mui-content").on("tap",".chair-btn",function(){
			var _this = this;
			// console.log(_this.classList[1])
			switch(_this.classList[1]){
				case "chair-btn1":
					moveUp(".chair1");
				break;
				case "chair-btn2":
					moveDown(".chair1");
				break;
				case "chair-btn3":
					moveUp(".chair2");
				break;
				case "chair-btn4":
					moveDown(".chair2");
				break;
				case "chair-btn5":
					deg = deg - 10;
					chairMoveUp(".chair1 .chair-02",deg);
				break;
				case "chair-btn6":
					deg = deg + 10;
					chairMoveDown(".chair1 .chair-02",deg);
				break;
				case "chair-btn7":
					deg2 = deg2 - 10;
					chairMoveUp(".chair2 .chair-02",deg2);
				break;
				case "chair-btn8":
					deg2 = deg2 + 10;
					chairMoveDown(".chair2 .chair-02",deg2);
				break;
				
			}
		});
		
		// 座椅向前移动
		function moveUp (className){
			var top = window.getComputedStyle(dq(className),null).top;
			// console.log(top)
			var tops = top.substring(0,top.lastIndexOf('p'))
			if(tops>277){
				top = (Number(tops)-5)+"px";
				tl1.to(dq(className), 1, { "top":top, ease: Power0.easeNone});
			}else{
				mui.toast("不能移动了！")
			}
		}
		// 座椅向后移动
		function moveDown (className){
			var top = window.getComputedStyle(dq(className),null).top;
			// console.log(top)
			var tops = top.substring(0,top.lastIndexOf('p'))
			if( tops < 317){
				top = (Number(tops)+5)+"px";
				tl1.to(dq(className), 1, { "top":top, ease: Power0.easeNone});
			}else{
				mui.toast("不能移动了！")
			}	
		}

		// 座椅靠背向前移动
		function chairMoveUp(className,deg){
			if(deg >-30){
				tl1.to(dq(className), 1, {  rotationX: (deg)+"deg", transformOrigin: "50% 0%", ease: Power0.easeNone,});	
			}else{
				deg = -30;
				mui.toast("不能移动了！")
			}
		}
		// 座椅靠背向后移动
		function chairMoveDown(className,deg){
			if(deg < 30){
				tl1.to(dq(className), 1, {  rotationX: (deg)+"deg", transformOrigin: "50% 0%", ease: Power0.easeNone,});	
			}else{
				deg = 30;
				mui.toast("不能移动了！")
			}
		}
		
	}
}